﻿using System;
using System.IO;
using System.Xml;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace svnxf
{
  class Utils
  {

    public static string GetFileContents(string path)
    {
      string fileContents;
      var helpFilePath = path;

      if (File.Exists(helpFilePath))
        using (var sr = new StreamReader(helpFilePath))
        {
          fileContents = sr.ReadToEnd();
        }
      else
        fileContents = "File not found";

      return fileContents;
    }

    public static string GetUrlDomain(string url)
    {
      string results = string.Empty;
      var match = Regex.Match(url, @"^http[s]?[:/]+[^/]+");
      if (match.Success)
        results = match.Captures[0].Value;

      return results;
    }

    public static bool DeleteDirectory(string path)
    {
      if (Directory.Exists(path))
        try
        {
          ClearAttributes(path);
          Directory.Delete(path, true);
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex.Message);
        }
      return true;
    }

    public static void CreateDirectory(string directory)
    {
      Directory.CreateDirectory(directory);
    }

    public static void ClearAttributes(string currentDir)
    {
      if (!Directory.Exists(currentDir)) return;

      var subDirs = Directory.GetDirectories(currentDir);
      foreach (var dir in subDirs)
        ClearAttributes(dir);

      var files = Directory.GetFiles(currentDir);
      foreach (var file in files)
        File.SetAttributes(file, FileAttributes.Normal);
    }

    public static string[] RegexSplit(string input, string pattern)
    {
      return Regex.Split(input, pattern)
        .Where(item => !string.IsNullOrEmpty(item)).ToArray();
    }

    public static string RegexReplace(string input, string pattern, string replacement)
    {
     return Regex.Replace(input, pattern, replacement);
    }
    
    public static XmlReader SvnCommand(string argument)
    {
      XmlReader xmlReader = null;
      try
      {
        var command = new Process();
        command.EnableRaisingEvents = false;
        command.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
        command.StartInfo.FileName = "svn.exe";
        command.StartInfo.Arguments = argument;
        command.StartInfo.UseShellExecute = false;
        command.StartInfo.RedirectStandardOutput = true;
        command.Start();

        Stream svnStream = command.StandardOutput.BaseStream;
        xmlReader = XmlReader.Create(svnStream);
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message + " = SVN.exe");
        Environment.Exit(0);
      }
      return xmlReader;
    }
  }
}